package com.itisyue.fun.utils;

import com.alibaba.fastjson.JSONObject;
import com.itisyue.fun.dto.FileDownloadDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.Base64;

public class EncryptUtil {
	private static Logger log = LoggerFactory.getLogger(EncryptUtil.class);
	public static String fileDownloadEncrypt2(String url, String password, long timestamp)  throws Exception {
		FileDownloadDTO fileDownloadDTO = new FileDownloadDTO(url, timestamp);
		//加密
		String encrypt = Sm2UtilBack.encrypt(JSONObject.toJSONString(fileDownloadDTO), password);
		String encodeToUrlSafeString = Base64.getUrlEncoder().encodeToString(encrypt.getBytes());
//		String encodeToUrlSafeString = Base64Utils.encodeToUrlSafeString(encrypt.getBytes());
		return encodeToUrlSafeString;
	}
	public static String fileDownloadEncrypt(String url, String password)  throws Exception {
		FileDownloadDTO fileDownloadDTO = new FileDownloadDTO(url);
		//加密
		String encrypt = Sm2UtilBack.encrypt(JSONObject.toJSONString(fileDownloadDTO), password);
		String encodeToUrlSafeString = Base64.getUrlEncoder().encodeToString(encrypt.getBytes());
//		String encodeToUrlSafeString = Base64Utils.encodeToUrlSafeString(encrypt.getBytes());
		return encodeToUrlSafeString;
	}
	public static String strEncrypt(String text, String password)  throws Exception {
		//加密
		String encrypt = Sm2UtilBack.encrypt(text, password);
		String encodeToUrlSafeString = Base64.getUrlEncoder().encodeToString(encrypt.getBytes());
//		String encodeToUrlSafeString = Base64Utils.encodeToUrlSafeString(encrypt.getBytes());
		return encodeToUrlSafeString;
	}
	public static String strDecrypt(String encodeText, String password)  throws Exception {
		// 解密
		byte[] decodeFromUrlSafeString = Base64.getUrlDecoder().decode(encodeText);
//		byte[] decodeFromUrlSafeString = Base64Utils.decodeFromUrlSafeString(encodeText);
		String decryptStr = Sm2UtilBack.decrypt(new String(decodeFromUrlSafeString), password);
		return decryptStr;
	}
	
//	public static String strDruidEncrypt(String text)  throws Exception {
//		/**
//		 * druid 加密，生成秘钥
//			cd /d D:\local_repository\com\alibaba\druid\1.1.20
//			cd D:\\local_repository\\com\\alibaba\\druid\\1.1.20
//			java -cp druid-1.1.20.jar com.alibaba.druid.filter.config.ConfigTools xx
//		 */
//
//		System.out.println("明文信息:"+text);
//		String[] keyPair = ConfigTools.genKeyPair(512);//512
//
//		//私钥
//		String privateKey = keyPair[0];
//		//公钥
//		String publicKey = keyPair[1];
//		//用私钥加密后的密文
//		String encodeText = ConfigTools.encrypt(privateKey, text);
//
//		System.out.println("privateKey:"+privateKey);
//		System.out.println("publicKey:"+publicKey);
//		System.out.println("password:"+encodeText);
//
//		return encodeText;
//
//	}
//	public static String strDruidDecrypt(String encodeText) throws Exception{
//		String[] keyPair = ConfigTools.genKeyPair(512);//512
//		//私钥
//		String privateKey = keyPair[0];
//		//公钥
//		String publicKey = keyPair[1];
//
//		String decryptPassword = ConfigTools.decrypt(publicKey, encodeText);
//		System.out.println("解密后:"+decryptPassword);
//
//		return decryptPassword;
//	}
	
	
	public static void fileEncryptWithPath(String src, String target, String secret) throws Exception{
		fileEncryption(new File(src), new File(target), secret);
	}
	public static void fileEncryption(File src, File target, String secret) throws Exception{
		InputStream inputStream = new BufferedInputStream(new FileInputStream(src));
		OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(target));
		int data = -1;
		try{
			while((data=inputStream.read())>-1){
				outputStream.write(data + Math.abs(secret.hashCode()));
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally {
			if(outputStream!=null){
				outputStream.close();
			}
			if(inputStream!=null){
				inputStream.close();
			}
		}
	}
	
	public static void fileDecryptWithPath(String src, String target, String secret) throws Exception{
		fileDecrypt(new File(src), new File(target), secret);
	}
	public static void fileDecrypt(File src, File target, String secret) throws Exception{
		InputStream inputStream = new BufferedInputStream(new FileInputStream(src));
		OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(target));
		int data = -1;
		try{
			while((data=inputStream.read())>-1){
				outputStream.write(data - Math.abs(secret.hashCode()));
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally {
			if(outputStream!=null){
				outputStream.close();
			}
			if(inputStream!=null){
				inputStream.close();
			}
		}
	}
	
	public static byte[] fileDecryptToBytes(String src, String secret) throws Exception{
		FileInputStream fis = new FileInputStream(src);
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		
//		byte[] buf = new byte[1024];
		int data = -1;
		try{
			while((data = fis.read())>-1){
				bos.write(data - Math.abs(secret.hashCode()));
			}
//			for(int readNum;(readNum = fis.read(buf)) !=-1; ){
//				bos.write(buf, 0, readNum);
//			}
		}catch(Exception e){
			e.printStackTrace();
		}
		byte[] byteArray = bos.toByteArray();
		if(fis!=null){
			fis.close();
		}
		if(bos!=null){
			bos.close();
		}
		return byteArray;
		
	}
	public static void EncryptTest() throws Exception{
		//druidEncrtpy();

		String secret  = "abc123def";

//		File src = new File("D:\\data\\media\\signature\\420000190011113333-2-20220708095346044.jpg");
//		File target = new File("D:\\data\\media\\signature\\420000190011113333-2-20220708095346044-encrypt.jpg");
//		fileEncryption(src, target, secret);

		//File enSrc = new File("D:\\data\\media\\signature\\420000190011113333-2-20220708102636018.jpg");
		//File deTarget = new File("D:\\data\\media\\signature\\420000190011113333-2-20220708102636018-de.jpg");
		//fileDecrypt(enSrc, deTarget, secret);

		String strDruidEncrtpt = fileDownloadEncrypt("/data/media/writeHand/2022/07/29/123456789012345.jpg", "");
		System.out.println(strDruidEncrtpt);

		String strDruidDecrypt = strDecrypt(strDruidEncrtpt, "");
		System.out.println(strDruidDecrypt);
	}

	public static void fileStoreTest() throws Exception{
		String pub = "MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEWuTGiihcA0P09ansdBX0X7R2OOACTEsRYwpUc97r8yq4iIHvM2MjklumD8wJQvdDMjsSMUdwmWGjHm8r11bZgQ==";
		String url = EncryptUtil.fileDownloadEncrypt("/home/nasdisk/ecl/temp/fileStore/89c2fb34b53adb2251a44182696e6b3a.zip", pub);
//		String url = EncryptUtil.fileDownloadEncrypt("/home/nasdisk/ecl/video/2024/06/20/441463-240620115351764380.mp4", pub);
		String prefix = "https://ecl.creditcard.cmbc.com.cn/ecl/media/common/view/";
		System.out.println(prefix+url);
	}
	public static void main(String[] args) throws Exception {
		fileStoreTest();

//		if(未超时){
//			if(异地){
//				if(无异地权限){
//					提示无异地权限
//				}else{
//					正常走
//				}
//			}else{
//				正常走
//			}
//		}else{
//			提示超时
//		}


	}

}
